package com.greenhouse.mapper.environment;

import com.greenhouse.model.Equipment;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface EquipmentMapper {
   /**
    * 创建名为name的用户设备表
    *
    * @param name 表名
    * @return 操作结果
    */
   @Insert("create table if not exists ${name} (" +
           "id int primary key auto_increment COMMENT '主键，自动递增'," +
           "greenhouse_id int COMMENT '大棚ID'," +
           "name varchar(255) not null COMMENT '设备名称'," +
           "status int COMMENT '设备状态'," +
           "create_time datetime COMMENT '创建日期'," +
           "start_time datetime COMMENT '开启时间'," +
           "end_time datetime COMMENT '关闭时间'," +
           "total_runtime varchar(50) COMMENT '总共运行时间（秒）'" +
           ")")
   Integer createUserEquipment(@Param("name") String name);

   /**
    * 向名为name的用户设备表中插入数据
    *
    * @param name         表名
    * @return 操作结果
    * @Param greenhouseId 大棚ID
    * @Param create_time 创建时间
    */
   @Insert("insert into ${tableName} " +
           "(greenhouse_id,name,status,create_time) " +
           "values" +
           "(#{greenhouseId},#{name},0,NOW())")
   Integer insertDataToExist(String tableName,Integer greenhouseId, String name);

   /**
    * 删除大棚ID为greenhouseId的用户设备表
    *
    * @param greenhouseId 大棚ID
    */
   @Delete("delete from ${name} where greenhouse_id = #{greenhouseId}")
   Integer deleteUserEquipment(String name,Integer greenhouseId);

   /**
    * 删除设备
    * @param id
    * @param name
    * @return
    */
   @Delete("delete from ${name} where id = #{id}")
   Integer deleteEquipment(String name, Integer id);

   /**
    * 查看设备运行状态
    * @param id
    * @param name
    * @return
    */
   @Select("select status from ${name} where id = #{id}")
   Integer selectStatus(String name,Integer id);

   List<Equipment> selectEquipments(Integer greenhouseId,String name);

   Equipment selectEquipment(String name,Integer id);

   @Update("update ${name} set name = #{equipment} where id = #{id}")
   Integer updateEquipment(String name, String equipment, Integer id);

   /**
    * 开启设备
    * @param name
    * @param id
    * @return
    */
   @Update("update ${name} set start_time = NOW(),status = 1,end_time = null where id = #{id}")
   Integer startEquipment(String name, Integer id);

   /**
    * 关闭设备
    * @param name
    * @param id
    * @return
    */
   @Update("update ${name} set end_time = NOW(),status = 0 where id = #{id}")
   Integer stopEquipment(String name, Integer id);

   /**
    * 修改设备运行时间
    */
   @Update("update ${name} set total_runtime = #{totalRuntime} where id = #{id}")
   Integer updateRunTime(String name, Integer id,String totalRuntime);
}
